package roslan.musicbox.server.data;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import roslan.musicbox.data.AbstractMusicTag;
import roslan.musicbox.exception.TagElementException;
import roslan.musicbox.exception.TagVersionFormatException;

public class ServerMusicTag extends AbstractMusicTag{
	private String fullpath;
	private MusicTagVersion version;
	
	public ServerMusicTag clone(){
		ServerMusicTag tag = new ServerMusicTag();
		tag.title = this.title;
		tag.album = this.album;
		tag.year = this.year;
		tag.genre = this.genre;
		tag.artist = this.artist;
		tag.comment = this.comment;
		tag.fullpath = this.fullpath;
		tag.trackNumber = this.trackNumber;
		tag.version = this.version.clone();
		return tag;
	}
	
	public String getFullpath(){
		return fullpath;
	}
	
	public MusicTagVersion getVersion(){
		return version;
	}
	
	public void setFullpath(String fullpath){
		this.fullpath = fullpath;
	}
	
	public void setVersion(MusicTagVersion version){
		this.version = version;
	}
	
	public String toString(){
		return "[Title: "+title+"], "+
			   "[Album: "+album+"], "+
			   "[Year: "+year+"], "+
			   "[Artist: "+artist+"], "+
			   "[Genre: "+genre+"], "+
			   "[TrackNumber: "+trackNumber+"], "+
			   "[Comment: "+comment+"], "+
			   "[Fullpath: "+fullpath+"], "+
			   "[Version: "+version+"].";
	}
	
	public static ServerMusicTag getTagFromElement(Element e) throws TagElementException{
		if(!e.getNodeName().equals("MusicTag")){
			throw new TagElementException();
		}
		ServerMusicTag tag = new ServerMusicTag();

		Element eTitle = (Element)e.getElementsByTagName("Title").item(0);
		Text tTitle = (Text)eTitle.getFirstChild();
		if(tTitle == null){
			tag.title = "";
		}else{
			tag.title = tTitle.getTextContent();
		}
		
		Element eAlbum = (Element)e.getElementsByTagName("Album").item(0);
		Text tAlbum = (Text)eAlbum.getFirstChild();
		if(tAlbum == null){
			tag.album = "";
		}else{
			tag.album = tAlbum.getTextContent();
		}
		
		Element eYear = (Element)e.getElementsByTagName("Year").item(0);
		Text tYear = (Text)eYear.getFirstChild();
		if(tYear == null){
			tag.year = "";
		}else{
			tag.year = tYear.getTextContent();
		}
		
		Element eGenre = (Element)e.getElementsByTagName("Genre").item(0);
		Text tGenre = (Text)eGenre.getFirstChild();
		if(tGenre == null){
			tag.genre = "";
		}else{	
			tag.genre = tGenre.getTextContent();
		}
		
		Element eArtist = (Element)e.getElementsByTagName("Artist").item(0);
		Text tArtist = (Text)eArtist.getFirstChild();
		if(tArtist == null){
			tag.artist = "";
		}else{
			tag.artist = tArtist.getTextContent();
		}

		Element eTrackNumber = (Element)e.getElementsByTagName("TrackNumber").item(0);
		Text tTrackNumber = (Text)eTrackNumber.getFirstChild();
		if(tTrackNumber == null){
			tag.trackNumber = "";
		}else{
			tag.trackNumber = tTrackNumber.getTextContent();
		}
		
		Element eComment = (Element)e.getElementsByTagName("Comment").item(0);
		Text tComment = (Text)eComment.getFirstChild();
		if(tComment == null){
			tag.comment = "";
		}else{
			tag.comment = tComment.getTextContent();
		}
		
		Element eFullpath = (Element)e.getElementsByTagName("Fullpath").item(0);
		Text tFullpath = (Text)eFullpath.getFirstChild();
		if(tFullpath == null) throw new TagElementException();
		tag.fullpath = tFullpath.getTextContent();
		
		Element eVersion = (Element)e.getElementsByTagName("Version").item(0);
		Text tVersion = (Text)eVersion.getFirstChild();
		if(tVersion == null) throw new TagElementException();
		try {
			tag.version = MusicTagVersion.getTagVersionFromInfo(tVersion.getTextContent());
		}catch (TagVersionFormatException e1) {
			throw new TagElementException();
		}
		
		return tag;
	}
	public Element getElement(Document doc){
		Element e = doc.createElement("MusicTag");
		
		Element eTitle = doc.createElement("Title");
		Text tTitle = doc.createTextNode(title);
		eTitle.appendChild(tTitle);
		e.appendChild(eTitle);
		
		Element eAlbum = doc.createElement("Album");
		Text tAlbum = doc.createTextNode(album);
		eAlbum.appendChild(tAlbum);
		e.appendChild(eAlbum);
		
		Element eYear = doc.createElement("Year");
		Text tYear = doc.createTextNode(year);
		eYear.appendChild(tYear);
		e.appendChild(eYear);
		
		Element eGenre = doc.createElement("Genre");
		Text tGenre = doc.createTextNode(genre);
		eGenre.appendChild(tGenre);
		e.appendChild(eGenre);
		
		Element eArtist = doc.createElement("Artist");
		Text tArtist = doc.createTextNode(artist);
		eArtist.appendChild(tArtist);
		e.appendChild(eArtist);
		
		Element eComment = doc.createElement("Comment");
		Text tComment = doc.createTextNode(comment);
		eComment.appendChild(tComment);
		e.appendChild(eComment);
		
		Element eTrackNumber = doc.createElement("TrackNumber");
		Text tTrackNumber = doc.createTextNode(trackNumber);
		eTrackNumber.appendChild(tTrackNumber);
		e.appendChild(eTrackNumber);
		
		Element eFullpath = doc.createElement("Fullpath");
		Text tFullpath = doc.createTextNode(fullpath);
		eFullpath.appendChild(tFullpath);
		e.appendChild(eFullpath);
		
		Element eVersion = doc.createElement("Version");
		Text tVersion = doc.createTextNode(version.toString());
		eVersion.appendChild(tVersion);
		e.appendChild(eVersion);
		
		return e;
	}
}
